﻿using App.Entity;
using App.IRepository;
using App.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace App.Services
{
  public   class ItemsApp
    {
        private ISys_ItemsRepository service = new Sys_ItemsRepository();
        /// <summary>
        /// 获取集合
        /// </summary>
        /// <returns></returns>
        public List<Sys_Items> GetList()
        {
            return service.QueryAll();
        }
        /// <summary>
        /// 根据Id获取实体
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public Sys_Items GetForm(string keyValue)
        {
            return service.QuerySingle(c => c.Id == keyValue);
        }
        /// <summary>
        /// 删除选项
        /// </summary>
        /// <param name="keyValue">Id</param>
        public void DeleteForm(string keyValue)
        {
            if (service.QueryCount(t => t.ParentId.Equals(keyValue)) > 0)
            {
                throw new Exception("删除失败！操作的对象包含了下级数据。");
            }
            else
            {
                service.Delete(t => t.Id == keyValue);
            }
        }
        /// <summary>
        /// 添加/修改选项
        /// </summary>
        /// <param name="itemsEntity"></param>
        /// <param name="keyValue"></param>
        public void SubmitForm(Sys_Items itemsEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                itemsEntity.Modify(keyValue);
                service.Update(itemsEntity);
            }
            else
            {
                itemsEntity.Create();
                service.Insert(itemsEntity);
            }
        }
    }
}
    

